package cn.gcBlock.rest;

import cn.gcBlock.service.BlockCurrencyService;
import cn.gcBlock.service.BlockNodeService;
import cn.gcBlock.service.BlockNodeTransService;
import cn.gcBlock.po.BlockCurrency;
import cn.gcBlock.po.constant.NodeType;
import cn.gcBlock.dto.BlockCurrencyQueryCriteria;
import cn.gcBlock.dto.NodeDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author jie
 * @date 2019-08-22
 */
@Api(tags = "WEB端发币签名管理")
@RestController
@RequestMapping
public class RestBlockCurrencyImpl implements RestBlockCurrencyService {

    @Autowired
    private BlockCurrencyService blockCurrencyService;
    @Autowired
    private BlockNodeService blockNodeService;
    @Autowired
    private BlockNodeTransService blockNodeTransService;
    @ApiOperation(value = "查询币种信息列表")
    public ResponseEntity getBlockCurrency(BlockCurrencyQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity(blockCurrencyService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    @ApiOperation(value = "发币")
    public ResponseEntity create(@Validated @RequestBody BlockCurrency resources) {
        NodeDTO signNode = blockNodeService.registerBlockAddress(resources.getSymbol());//符号
        signNode.setCoin(resources.getCoin());//数量
        signNode.setType(NodeType.CREATION.getCode());
        signNode.setToPublicKey(signNode.getPublicKey());
        signNode.setPrivateKey(signNode.getPrivateKey());
        resources.setPrivateKey(signNode.getPrivateKey());
        signNode.setPrivateKey(signNode.getPrivateKey());
        signNode.setDataBlock(resources.getDataBlock());//声明
        //进行签名
        NodeDTO signedNode = blockNodeService.sign(signNode);
        blockNodeTransService.transBlock(signedNode);
        //自动创生第一个区块

        resources.setPublicKey(signNode.getPublicKey());
        return new ResponseEntity(blockCurrencyService.create(resources), HttpStatus.CREATED);
    }
    @ApiOperation(value = "删除管理器中的币种信息")
    public ResponseEntity delete(@PathVariable int id) {
        blockCurrencyService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}